﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DealKhuyenMaiV2.com.Models;
using System.Web.Security;
using PagedList;
namespace DealKhuyenMaiV2.com.Controllers
{
    public abstract class BaseController : Controller
    {
        //
        // GET: /Base/
        private DKMDatabaseEntities dbConText;

        public DKMDatabaseEntities DbConText
        {
            get { return dbConText; }
            
        }
        public BaseController()
        {
            dbConText = new DKMDatabaseEntities();

            var result = from p in DbConText.Products where p.StatusProduct.NameStatus == "Đã duyệt" select p;
         
            List<Product> ListPro = new List<Product>();
            foreach (Product p in result)
                ListPro.Add(p);
            ViewData["ListProduct"] = ListPro.ToList();
            GetListProvince("");
            //Lay list catalogy xuống guestLayout
            var ResultCatalogy = from p in DbConText.Catalogies select p;
            List<Catalogy> ListCatalogy = new List<Catalogy>();
            foreach (Catalogy c in ResultCatalogy)
                ListCatalogy.Add(c);
            ViewBag.ListCatalogy = ListCatalogy;
        }


        public void GetListProvince(string value)
        {
            var ResultProvince = from p in DbConText.Provinces select p;
            string Selected="1";
            List<Province> ListProvince = ResultProvince.ToList();
            Province None = new Province();
            None.IDProvince = -1;
            None.NameProvince = "Toàn Quốc";
            ListProvince.Insert(0, None);
            if (value == "")
            {
                try
                {
                    Selected = System.Web.HttpContext.Current.Request.Cookies["ChooseCity"].Value;
                    
                }
                catch (NullReferenceException e)
                {
                    Selected = "1";
                }
            }
            else
                Selected = value;
            ViewBag.ListProvince = new SelectList(ListProvince.ToList(), "IDProvince", "NameProvince", Selected);
        }
    }
}
